package com.project.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {
    //spring封装rabiitmq的代码 RabbitTemplate
//注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }


    //创建队列  (默认)
    @Bean
    public Queue infoQueue(){
        return new Queue("infoQueue");
    }
    //  主题队列
    @Bean
    public Queue infoQueueTopic(){
        return new Queue("infoQueueTopic");
    }
    //  扇形队列1
    @Bean
    public Queue infoQueueOne(){
        return new Queue("infoQueueOne");
    }
    //  扇形队列2
    @Bean
    public Queue infoQueueTwo(){
        return new Queue("infoQueueTwo");
    }
    // 私信队列
    @Bean
    public  Queue  fourQueue(){
        return  new Queue("fourQueue");
    }
    @Bean
    public Queue j187Queue(){
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "directExchangeDeadJ187");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","j187.dead");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
        return QueueBuilder.durable("j187Queue").withArguments(args).build();
    }
    @Bean
    public  Queue deadQueue(){
        return  new Queue("deadQueueJ187");
    }
    //创建交换机
    @Bean
    public DirectExchange directExchangeUser(){
        return new DirectExchange("directExchangeInfo");
    }
    //主题交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topicExchange");
    }
    //扇形交换机
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanoutExchange");
    }

    //交换机
    @Bean
    public HeadersExchange headersExchange(){
        return new HeadersExchange("headersExchange");
    }
    @Bean
    public DirectExchange directExchangeDead(){
        return new DirectExchange("directExchangeDeadJ187");
    }

    @Bean
    public DirectExchange directExchangeJ187(){
        return new DirectExchange("directExchangeJ187");
    }
    //交换机与队列进行绑定
    @Bean
    public Binding userQueueToDirectExchangeUser(Queue infoQueue,DirectExchange directExchangeUser){
        return    BindingBuilder.bind(infoQueue).to(directExchangeUser).with("info.i");
    }

    //主题
    @Bean
    public Binding userQueueTopicExchange(Queue infoQueueTopic,TopicExchange topicExchange){
        return    BindingBuilder.bind(infoQueueTopic).to(topicExchange).with("info.#");
    }
    //扇形1
    @Bean
    public Binding OneQueueToFanoutExchange(Queue infoQueueOne,FanoutExchange fanoutExchange){
        return    BindingBuilder.bind(infoQueueOne).to(fanoutExchange);
    }
    //扇形2
    @Bean
    public Binding twoQueueToFanoutExchange(Queue infoQueueTwo,FanoutExchange fanoutExchange){
        return    BindingBuilder.bind(infoQueueTwo).to(fanoutExchange);
    }
    // 死性交换机
    @Bean
    public Binding fourQueueToHeadersExchange(Queue  fourQueue  ,HeadersExchange headersExchange){
        HashMap<String,Object> map=new HashMap<>();
        map.put("orderKey","order");
        map.put("dateKey","2022-12-12");
        return  BindingBuilder.bind(fourQueue).to(headersExchange).whereAll(map).match();
    }

    @Bean
    public Binding j187QueueToDirectExchangeJ187(Queue j187Queue,DirectExchange directExchangeJ187){
        return BindingBuilder.bind(j187Queue).to(directExchangeJ187).with("j187");
    }
    @Bean
    public Binding deadQueueToDirectExchangeDead(Queue deadQueue,DirectExchange directExchangeDead){
        return BindingBuilder.bind(deadQueue).to(directExchangeDead).with("j187.dead");
    }
}
